# _*_ coding: utf-8 _*_
__author__ = 'yinlei'
__date__ = '2018-05-18 8:23'
from apps.bases.forms import BaseForm
from wtforms import StringField,IntegerField
from wtforms.validators import Regexp,EqualTo,ValidationError,InputRequired,Length
from utils import zlcache

class Signupform(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[345789]\d{9}',message='请输入正确格式的手机号码')])
    sms_captcha = StringField(validators=[Regexp(r'\d{6}',message='请输入正确的验证码')])
    username = StringField(validators=[Regexp(r'.{2,20}',message='请输入正确格式的用户名')])
    password1 = StringField(validators=[Regexp(r'[0-9a-zA-Z_\.]{6,20}')])
    password2 = StringField(validators=[EqualTo('password1',message='两次输入的图形验证码不一致')])
    graph_captcha = StringField(validators=[Regexp(r'\w{4}',message='请输入正确的图形验证码')])

    def validate_sms_captcha(self,field):

        sms_captcha = field.data
        telephone = self.telephone.data
        sms_captcha_mem = zlcache.get(telephone) if telephone else None
        if not sms_captcha_mem or sms_captcha_mem.lower() != sms_captcha.lower():
            raise ValidationError(message='短信验证码错误!')

    def validate_graph_captcha(self,field):
        graph_captcha = field.data
        graph_captcha_mem = zlcache.get(graph_captcha.lower()) if graph_captcha else None
        if not  graph_captcha_mem:
            raise ValidationError('图形验证码错误')

class SigninForm(BaseForm):
    telephone = StringField(validators=[Regexp(r'1[345789]\d{9}',message='请输入正确的手机号码')])
    password = StringField(validators=[Regexp(r'[a-zA-Z0-9_\.]{6,20}',message='请输入正确的面格式')])
    remember = StringField()


class AddPostForm(BaseForm):
    title = StringField(validators=[InputRequired(message='请输入标题'),Length(min=2,max=100,message='最少两个字符,最多一百个字符')])
    content = StringField(validators=[InputRequired(message='请输入内容')])
    board_id = IntegerField(validators=[InputRequired(message='请输入板块id')])


class AddCommentForm(BaseForm):
    content = StringField(InputRequired(message='请输入评论内容'))
    post_id = IntegerField(InputRequired(message='请输入帖子id'))
